$(document).ready(function(){
	var options = { 
        success: showMessage,
        beforeSend: function() {
			$('#btnSave').hide();
			$('#module-loading').show();
		},
        complete: function() {
			$('#btnSave').show();
			$('#module-loading').hide();
		},
        url:  $('#frmClinDiagEdit').attr('action')+'?set_ajax_view',
        dataType:  'json'      
    }; 
    // Bind form using 'ajaxForm' 
    $('#frmClinDiagEdit').ajaxForm(options);
	$('div#encounterPageContent').show();				
	
	$('#btnPriIcd10').bind('click',function(){
		curIcd10CodeId	=	'clinDiagPrimaryIcd10';	
		curIcd10NameId	=	'clinDiagPrimaryDiagnosis';
		showIcd10List(curIcd10CodeId, curIcd10NameId);
	});  
	$('#btnSecIcd10_1').bind('click',function(){
		curIcd10CodeId	=	'clinDiagSecondaryIcd10_1';	
		curIcd10NameId	=	'clinDiagSecondaryDiagnosis_1';
		showIcd10List(curIcd10CodeId, curIcd10NameId);
	});  
	$('#btnSecIcd10_2').bind('click',function(){
		curIcd10CodeId	=	'clinDiagSecondaryIcd10_2';	
		curIcd10NameId	=	'clinDiagSecondaryDiagnosis_2';
		showIcd10List(curIcd10CodeId, curIcd10NameId);
	}); 
		
	/*
	 ICD10 is optional
	  
	$('#clinDiagPrimaryIcd10').blur(function(){
		isNotEmpty($(this), _("Not yet input ICD 10 diagnosis"),"statusMessageClinDiag");
	});
	*/
	
});

function submitForm(){
	if(!isReadyToSubmitDiagnosis()){
		return;
	}
	if ($('#doctorId').val() == '' || $.trim($('#doctorName').val()) == ''){		
		$('#lblEditMessage').css('color', 'red').text(_("Fail! Please check the required fields"));
		$('#doctorName').css('border-color', 'red');
		return;
	} else {
		$('#doctorName').css('border-color', 'AAA');
	}	
	$('#divEditMessageBarLoading').show();
	$('#frmClinDiagEdit').submit();
}

function resetInputBorderColor(){
	$(":input").css("border-color", "#AAA");
}

function showMessage(response, status){
	if (status=='success' && response.saved) {
		$('#lblEditMessage').css('color', 'green').text(_("Save successful"));
		$('#encounterId').val(response.encId);
				
		resetInputBorderColor();						
	} else if (!response['saved'] && response['errorField']!='') {
		var obj	=	$("#" + response['errorField']);
		obj.css("border-color", "red").focus();
		
		$('span#statusMessageClinDiag').text(_("Disease ID is not exist in ICD 10 database")).show();		
		$('#btnSave').show();
	} else {
		$('#lblEditMessage').text(_("Save successful"));
	}
}


function isReadyToSubmitDiagnosis(){
	$('#lblEditMessage').css('color', 'green').text('');	
	/*
	 * Optional ICD
	 * 
	if(!isNotEmpty($('#clinDiagPrimaryIcd10'), _("Not yet input ICD 10 diagnosis"), "statusMessageClinDiag")){
		$('#lblEditMessage').css('color', 'red').text(_("Fail! Please check the required fields"));
		return false;
	}
	*/	
	return true;
}